package com.ZeroCarbon.controller;

import com.ZeroCarbon.domain.dto.QuestionInfoDTO;
import com.ZeroCarbon.domain.po.BalanceIntegral;
import com.ZeroCarbon.domain.po.UserBaseInfo;
import com.ZeroCarbon.entity.Result;
import com.ZeroCarbon.service.IAvatarService;
import com.ZeroCarbon.service.IBalanceIntegralService;
import com.ZeroCarbon.service.IBaseInfoService;
import com.ZeroCarbon.service.IQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户信息控制器
 *
 * @since 2024-10-14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final IBaseInfoService baseInfoService;

    private final IQuestionService questionService;

    private final IAvatarService avatarService;

    private final IBalanceIntegralService balanceIntegralService;

    /**
     * 保存基本信息
     *
     * @param userInfo 用户基本信息
     */
    @PostMapping("/save")
    public Result saveBaseInfo(@RequestBody UserBaseInfo userInfo) {
        try {
            baseInfoService.saveBaseInfo(userInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取基本信息
     */
    @GetMapping("/getBaseInfo")
    public Result getBaseInfo(@RequestParam(value = "userId", required = false) String userId) {
        try {
            return Result.success(baseInfoService.getBaseInfo(userId));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 保存问题
     *
     * @param questions 用户密保问题
     */
    @PostMapping("/saveQuestion")
    public Result saveQuestion(@RequestBody QuestionInfoDTO questions) {
        try {
            questionService.saveQuestion(questions);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取密保问题
     *
     * @param id 账号
     */
    @GetMapping("/getQuestion")
    public Result getQuestion(@RequestParam String id) {
        try {
            return Result.success(questionService.getQuestion(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新用户信息
     *
     * @param userBaseInfo 用户信息
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserBaseInfo userBaseInfo) {
        try {
            baseInfoService.updateUser(userBaseInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 保存用户头像
     */
    @PostMapping("/saveAvatar")
    public Result saveAvatar(@RequestBody MultipartFile avatar) {
        try {
            avatarService.saveAvatar(avatar);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户头像
     */
    @GetMapping("/getAvatar")
    public Result getAvatar() {
        try {
            return Result.success(avatarService.getAvatar(null));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户名称
     *
     * @param id 用户id
     */
    @GetMapping("/getUsername")
    public Result getUsername(@RequestParam String id) {
        try {
            return Result.success(baseInfoService.getUsername(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户头像
     */
    @GetMapping("/getAvatarById")
    public Result getAvatarById(@RequestParam String id) {
        try {
            return Result.success(avatarService.getAvatar(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取余额和积分
     */
    @GetMapping("/getBalanceIntegral")
    public Result getBalanceIntegral() {
        try {
            return Result.success(balanceIntegralService.getBalanceIntegral());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改余额和积分
     */
    @PostMapping("/updateBalanceIntegral")
    public Result updateBalanceIntegral(@RequestBody BalanceIntegral balanceIntegral) {
        try {
            balanceIntegralService.updateBalanceIntegral(balanceIntegral);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新账户余额和积分
     */
    @GetMapping("/addMoneyAndIntegral")
    public Result addMoneyAndIntegral(@RequestParam String id, @RequestParam double money, @RequestParam int integral) {
        try {
            balanceIntegralService.updateMoneyAndIntegral(id, money, integral);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取验证码
     */
    @GetMapping("/getBalanceVerificationCode")
    public Result getBalanceVerificationCode(@RequestParam double money) {
        try {
            balanceIntegralService.getBalanceVerificationCode(money);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 校验验证码
     */
    @GetMapping("/verifyBalanceVerificationCode")
    public Result verifyBalanceVerificationCode(@RequestParam String code) {
        try {
            balanceIntegralService.verifyBalanceVerificationCode(code);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
